window.onload = function () {
    form.on("submit(comment)",function (data) {
        var action = $(data.form).attr('action');
        var type = $(data.form).attr('method')
        $.ajax({
            type:type,
            data:data.field,
            dataType:'json',
            url:action,
            success:function (res) {
                var content  = res.data;
                var user = res.data.user;
                if(res.code===1){
                    var avatar = '/template/img/avatar.jpg';
                    var nickname = '游客';
                    if(user.avatar) avatar = user.avatar;
                    if(user.nickname){
                        nickname = user.nickname
                    }else if(user.username){
                        nickname = user.username
                    }
                    $(".base-comment-container .comment-ul").append(` <li class="layui-row layui-col-space15 list">
                                <div class="layui-col-md1">
                                    <img src="`+avatar+`" alt="" class="avatar">
                                </div>
                                <div class="layui-col-md11">
                                    <p class="fbtime">
                                        `+nickname+`
                                        <span class="create-date">`+content.create_time+`</span>
                                    </p>
                                    <p class="fbinfo">
                                       `+content.content+`
                                    </p>
                                </div>
                            </li>`);
                    $('.base-comment-container textarea').val('')
                }else {
                    layer.msg(res.msg);
                }
            },
            error:function (res) {
                layer.msg("网络出错了请稍后再试~！！")
            }
        })
        return false;
    });

    var current_page = 1;
    $(".load-comments").click(function () {
        var total = $(this).attr("data-total");
        var per_page = $(this).attr("data-per_page");
        //var current_page = $(this).attr("data-current_page");
        var last_page = $(this).attr("data-last_page");
        var url = $(this).attr("data-url");
        var arc_id = $(this).attr("data-arc_id");
        var topic_type = $(this).attr("data-topic_type");

        // var new_current_page = parseInt(current_page) + 1;
        current_page ++ ;

        if(current_page<=last_page){
            var load_index = layer.load(2,{
                success:function () {
                    $.ajax({
                        type:"get",
                        url:url,
                        data:{
                            page:current_page,
                            limit:per_page,
                            topic_id:arc_id,
                            topic_type:topic_type
                        },
                        success:function (res) {
                            if(res.data.length>0){
                                $.each(res.data,function (k,v) {
                                    var avatar = '/template/img/avatar.jpg';
                                    var nickname = '游客';
                                    if(v.user.avatar) avatar = v.user.avatar;
                                    if(v.user.nickname){
                                        nickname = v.user.nickname
                                    }else if(v.user.username){
                                        nickname = v.user.username
                                    }
                                    $(".base-comment-container .comment-ul").append(` <li class="layui-row layui-col-space15 list">
                                        <div class="layui-col-md1">
                                            <img src="`+avatar+`" alt="" class="avatar">
                                        </div>
                                        <div class="layui-col-md11">
                                            <p class="fbtime">
                                                `+nickname+`
                                                <span class="create-date">`+v.create_time+`</span>
                                            </p>
                                            <p class="fbinfo">
                                               `+v.content+`
                                            </p>
                                        </div>
                                    </li>`);
                                })
                            }
                            layer.close(load_index)
                        }
                    })
                }
            })
        }else {
            layer.msg("已经没有了!")
        }
       // console.log(total,per_page,current_page,last_page,url,arc_id)
    })
}